package org.db2.project.airline.service;

import org.db2.project.airline.dao.DAOException;
import org.db2.project.airline.dao.FlightClassPriceDAO;
import org.db2.project.airline.dao.impl.HibernateFlightClassPriceDAO;
import org.db2.project.airline.model.FlightClassPrice;
import org.db2.project.airline.model.FlightClassType;
import org.db2.project.airline.model.FlightPlan;
import org.db2.project.airline.utils.HibernateHelper;

import java.math.BigDecimal;

/**
 *
 */
public class FlightClassPriceService {

    private final FlightClassPriceDAO flightClassPriceDAO = new HibernateFlightClassPriceDAO();

    public FlightClassPriceService() {
        // empty constructor
    }

    public BigDecimal getFlightPrice(FlightPlan flightPlan, FlightClassType flightClassType) throws DAOException {

        try {
            HibernateHelper.beginTransaction();

            FlightClassPrice flightClassPrice = flightClassPriceDAO.find(flightPlan, flightClassType);

            HibernateHelper.commitTransaction();

            return flightClassPrice != null ? flightClassPrice.getPrice() : null;
        } catch (DAOException exc) {
            HibernateHelper.rollbackTransaction();

            throw exc;
        }
    }
}
